<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>产品分销管理</title>
    <link type="text/css" href="/supplier/public/pc/css/v1/base.css" rel="stylesheet">
    <link type="text/css" href="/supplier/public/pc/css/v1/home.css" rel="stylesheet">
    <link type="text/css" href="/supplier/public/pc/css/supplier_pub.css" rel="stylesheet">
    <link type="text/css" href="/tools/js/msgbox/msgbox.css" rel="stylesheet">
    <link type="text/css" href="/supplier/public/pc/css/supplier_back.css" rel="stylesheet">
    <script src="/supplier/public/pc/js/jquery.min.js"></script>
    <script src="/supplier/public/pc/js/laypage/laypage.js"></script>
    <script src="/tools/js/msgbox/msgbox.js"></script>
    {Common::js('common.js,choose.js')}
</head>
<body>
<div class="supplier-main-block">
    <div class="block-tab-container">
        <div class="block-tab-wrap-list">
            <div class="block-search-bar cl">
                <select name="channel_id" class="input-text w100 select fl channel_id">
                    {loop $channel $c}
                    <option value="{$c['id']}">{$c['title']}</option>
                    {/loop}
                </select>
                <select name="type_id" class="input-text w100 select fl type_id">
                    {loop $app_list $app}
                    <option class="{$app['class_name']}" value="{$app['id']}">{$app['modulename']}</option>
                    {/loop}
                </select>
                <div class="item">
                    <input name="keyword" type="text" class="input-text w300" placeholder="请输入产品名称" />
                    <input id="search_btn" type="button" class="btn btn-default ml-5" onclick="search_product()" value="查询">
                </div>
                <a class="fr refresh-btn" onclick="window.location.reload()" href="javascript:;">刷新</a>
            </div>
            <!-- 搜索栏 -->
            <div class="table-view-box">
                <table class="table-view-group" div_head=0rG0mD >
                    <thead>
                    <tr>
                        <th width="10%">选择</th>
                        <th width="10%">编号</th>
                        <th width="40%" class="name">名称</th>
                        <th width="20%">报价有效期</th>
                        <th width="10%">状态</th>
                        <th width="10%">操作</th>
                    </tr>
                    </thead>
                    <tbody class="product-list-container">

                    </tbody>
                </table>
            </div>
            <!-- 列表视图 -->

        </div>

        <div class="console-bottom-wrapper clearfix">
            <div class="console-col f-l">
                <a id="choose_all" class="btn btn-primary" href="javascript:;">全选</a>
                <a id="choose_diff" class="btn btn-primary" href="javascript:;">反选</a>
                <a id="product_upline" class="btn btn-primary" href="javascript:;">上架</a>
                <a id="product_offline" class="btn btn-primary" href="javascript:;">下架</a>
                <a id="product_check" class="btn btn-primary" href="javascript:;">提交审核</a>
            </div>
            <div class="page-container">

            </div>
        </div>
        <!-- 列表操作、翻页 -->
    </div>
</div>
<script>
    var SITEURL = "{$web_url}";
    $(function(){
        product_list(1)
        $('.type_id').change(function(){
            $("[name='keyword']").val('');
            product_list(1);
        });
        $('.channel_id').change(function(){
            var val = $('.channel_id option:selected').val();
            if(val>1){
                if(val==3){
                    $('.type_id').find("[value='2']").siblings().hide()
                    $('.type_id').find("[value='2']").attr('selected',true);
                }else{
                    $('.channel_none').hide();
                    $('.channel_show').show();
                    $('.type_id option').eq(0).attr('selected',true);
                }
            }else{
                $('.channel_none').show();
                $('.type_id option').eq(0).attr('selected',true);
            }

            product_list(1);
        });
        $('.block-tab-bar span').on('click',function(){
            $(this).addClass('on').parent().siblings('a').find('span').removeClass('on');
            product_list(1)
        });
    })
    function search_product()
    {
        product_list(1)
    }

    function product_list(page)
    {
        var channel_id = $('.channel_id option:selected').val();
        var type_id = $('.type_id option:selected').val();
        var keyword = $("[name='keyword']").val();
        $.ajax({
            data: {type_id:type_id,page:page,keyword:keyword,channel_id:channel_id},
            dataType: 'json',
            type: 'post',
            url: SITEURL+'product_list',
            success: function (data) {
                if(data.status){
                    var str = '';
                    var html = '';
                    for(var i=0;i<data.list.length;i++){
                        var row = data.list[i];
                        if(row.status==3||row.status==2){
                            if(channel_id==1){
                                str = row.ishidden==0?'<span class="col-red">下架</span>':'<span class="color-green">上架</span>';
                            }else{
                                str = row.channel_status?'<span class="col-red">下架</span>':'<span class="col-green">上架</span>';
                            }
                        }else{
                            str = row.status==0||row.status==4?'<span class="col-yellow">提审</span>':'<span class="col-orange">审核中</span>';
                        }
                        html += '<tr>';
                        html += '<td><input type="checkbox" name="select" value="'+row.id+'"/></td>';
                        html += '<td>'+row.id+'</td>';
                        html += '<td><div class="name">'+row.title+'</div></td>';
                        html += '<td>'+row.time_validity+'</td>';
                        html += '<td>'+row.status_name+'</td>';
                        html += '<td><a onclick="operation($(this),'+row.id+')" class="color-green" data-ishidden="'+row.ishidden+'" data-channel_status="'+row.channel_status+'" data-status="'+row.status+'" data-id="'+row.id+'" href="javascript:;">'+str+'</a></td>';
                        html += '</tr>';
                    }
                    $('.product-list-container').html(html);
                    if(data.total_page>1){
                        pages(data.total_page, page);
                    }
                }else{
                    $('.product-list-container').html('');
                }
            }
        })
    }

    function pages(total, page)
    {
        laypage({
            cont: $('.page-container'),
            pages: total,
            curr: page,
            skip: true,
            skin: '#14cc5c',
            groups: 2,
            first: '首页',
            last: '尾页',
            jump:function (obj, first)
            {
                if(!first){
                    page = obj.curr;
                    product_list(page);
                }
            }
        });
    }

    var piliang=''

    function operation(obj, product_id)
    {
        var status = obj.attr('data-status');
        if(status==1){
            ST.Util.showMsg('审核中不可操作', 5, 1000);
            return false;
        }
        var ishidden = obj.attr('data-ishidden');
        var channel_status = obj.attr('data-channel_status');
        ishidden = ishidden==0?1:0;
        var type_id = $('.type_id option:selected').val();
        var channel_id = $('.channel_id option:selected').val();
        ajax_submit(product_id,ishidden,type_id,status,channel_id,channel_status,obj,0)
    }

    function ajax_submit(product_id,ishidden,type_id,status,channel_id,channel_status,obj,is_all){
        $.ajax({
            data: {product_id:product_id,ishidden:ishidden,type_id:type_id,status:status,channel_id:channel_id,channel_status:channel_status},
            dataType: 'json',
            type: 'post',
            url: SITEURL+'product_operation',
            success: function (data) {
                if(data.status){
                    var str = '';
                    if(channel_id>1){
                        obj.attr('data-channel_status',channel_status=='true'?'false':'true');
                        str = channel_status=='true'?'<span class="color-green">上架</span>':'<span class="col-red">下架</span>';
                    }else{
                        status = status==0||status==4?1:3;
                        str = ishidden==0?'<span class="col-red">下架</span>':'<span class="color-green">上架</span>';
                        if(status==1){
                            str = '<span class="col-orange">审核中</span>';
                        }
                    }
                    obj.attr({'data-ishidden':ishidden,'data-status':status}).html(str);
                    ST.Util.showMsg(data.msg, 4, 1000);
                }else{
                    if(is_all!=1){
                        ST.Util.showMsg(data.msg, 5, 1000);
                    }else{
                        ST.Util.showMsg('操作成功', 4, 1000);
                    }
                }
            }
        })
    }

    function choose_all_check(){
        var selectedValues = $('input[name="select"]:checked').map(function(){
            return $(this).val();
        }).get();
        if(selectedValues.length==0){
            ST.Util.showMsg('请选择产品', 5, 1000);
            return false
        }
    }

</script>
<script>
    $(function(){
        $('#choose_all').click(function () {
            $('input[name="select"]').prop('checked', true);
        })
        $('#choose_diff').click(function () {
            $('input[name="select"]').prop('checked', false);
        })

        $('#product_upline').click(function () {
            choose_all_check()
            $('a[onclick^="operation($(this)"]').each(function() {
                let me =$(this)
                let product_id = me.data('id')
                let ishidden = 0
                let type_id = $('.type_id option:selected').val();
                let channel_id = $('.channel_id option:selected').val();
                let channel_status = me.data('channel_status');
                let status = me.data('status');
                ajax_submit(product_id,ishidden,type_id,status,channel_id,channel_status,me,1)
            });
        })

        $('#product_check').click(function () {
            choose_all_check()
            $('a[onclick^="operation($(this)"]').each(function() {
                let me =$(this)
                let product_id = me.data('id')
                let ishidden = 0
                let type_id = $('.type_id option:selected').val();
                let channel_id = $('.channel_id option:selected').val();
                let channel_status = me.data('channel_status');
                let status = me.data('status');
                ajax_submit(product_id,ishidden,type_id,status,channel_id,channel_status,me,1)
            });
        })

        $('#product_offline').click(function () {
            choose_all_check()
            $('a[onclick^="operation($(this)"]').each(function() {
                let me =$(this)
                let product_id = me.data('id')
                let ishidden = 1
                let type_id = $('.type_id option:selected').val();
                let channel_id = $('.channel_id option:selected').val();
                let channel_status = me.data('channel_status');
                let status = me.data('status');
                ajax_submit(product_id,ishidden,type_id,status,channel_id,channel_status,me,1)
            });
        })

    })
</script>
</body>
</html>